--
-- Author: shunguo.chen
-- Date: 2019-05-16 16:02:00
--
-- LegendModel  传奇武器model
--

local LegendInfo = import(".LegendInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._legendInfo = LegendInfo.new()

end

function ClassRef:getInfo()
    return self._legendInfo
end

function ClassRef:clearSkillCache()
    return self._legendInfo:clearSkills()
end

function ClassRef:clearLegendNew()
    local legends = self._legendInfo.legends
    for i,legend in ipairs(legends) do
        if legend:isNew() then
            legend:setIsNew(false)
        end
    end
    
    UD.badge:setNum(Constant.FUNC_TYPE_LEGEND, 0)
end

-- 获取当前穿戴legend
function ClassRef:getWearLegend()
    return self._legendInfo:getWearLegend()
end

-- 检测红点
function ClassRef:checkRedPoint()
    local hasNew = false
    local legends = self._legendInfo.legends
    for i,legend in ipairs(legends) do
        if legend:hasRedPoint() then
            hasNew = true
            break
        end
    end

    -- print("LegendModel.checkRedPoint", hasNew, hasNew and 1 or 0)
    UD.badge:setNum(Constant.FUNC_TYPE_LEGEND, hasNew and 1 or 0)
end

------------------------ receive ------------------------
function ClassRef:S2C_NewLegendNotify(msg)
    -- dump(msg, "S2C_NewLegendNotify")
    local legend = self._legendInfo:activeLegend(msg.LegendInfo)
    self._legendInfo:updateStarMapActive(msg.ActiveStarMapIDs)

    app:sendMsg("LegendController", "openNewView", legend)
    self:checkRedPoint()
end

------------------------ request ------------------------
-- 请求神器数据,只请求一次
function ClassRef:C2S_GetLegendInfo(cb)
    if self._legendInfo:isInit() then
        executeFunc(cb, self._legendInfo)
        return
    end

    app.session:request("pb_legend.C2S_GetLegendInfo",{},function(msg)
        -- dump(msg.Info, "C2S_GetLegendInfo", 5)
        self._legendInfo:init(msg.Info)
        self:checkRedPoint()
        executeFunc(cb, self._legendInfo)
    end)
end

-- 装备/卸下神器
-- legend 神器
-- isWithFashion 是否穿戴神器最高级外观
function ClassRef:C2S_EquipLegend(legend, isWithFashion)
    local params = {ID=legend:getId(), WearingOrNot=isWithFashion}
    -- dump(params, "C2S_EquipLegend params")
    app.session:request("pb_legend.C2S_EquipLegend",params,function(msg)
        -- dump(msg, "C2S_EquipLegend msg", 10)

        self._legendInfo:updateLegendWear(legend)
        app:sendMsg("LegendController", "onLegendEquip", legend)
    end)
end

-- 神器升级
function ClassRef:C2S_LevelUpLegend(legend, num)
    local params = {LegendID=legend:getId(), Num=num}
    app.session:request("pb_legend.C2S_LevelUpLegend",params,function(msg)
        -- dump(msg, "C2S_LevelUpLegend", 10)

        legend:updateLvExp(msg.NewLevel, msg.NewExp, msg.LegendInfo)
        if #msg.ActiveStarMapIDs > 0 then
            self._legendInfo:updateStarMapActive(msg.ActiveStarMapIDs)
        end
        if msg.LegendInfo then
            self:checkRedPoint()
        end
        app:sendMsg("LegendController", "onLegendLvup", legend)
    end)
end

-- 神器重置(重置到刚刚获得的状态)
function ClassRef:C2S_RecycleLegendLevel(legend)
    local params = {ID=legend:getId()}
    app.session:request("pb_legend.C2S_RecycleLegendLevel",params,function(msg)
        -- dump(msg, "C2S_RecycleLegendLevel", 10)

        legend:initMsg(msg.Legend)
        self:checkRedPoint()
        app:sendMsg("LegendController", "onLegendReset", legend)
    end)
end

-- 神器技能激活
function ClassRef:C2S_ActiveLegendSkill(legend, skill)
    local params = {LegendID=legend:getId(), SkillID=skill:getLegendTeam()}
    app.session:request("pb_legend.C2S_ActiveLegendSkill",params,function(msg)
        -- dump(msg, "C2S_ActiveLegendSkill", 10)

        legend:activeSkill(skill)
        self:checkRedPoint()
        app:sendMsg("LegendController", "onLegendSkillActive", legend, skill)
    end)
end

-- 神器技能装备/卸下
function ClassRef:C2S_ChooseLegendSkill(legend, skill)
    local params = {LegendID=legend:getId(), SkillID=skill:getLegendTeam()}
    app.session:request("pb_legend.C2S_ChooseLegendSkill",params,function(msg)
        -- dump(msg, "C2S_ChooseLegendSkill", 10)

        legend:updateSkill(skill)
        app:sendMsg("LegendController", "onLegendSkillChoose", legend, skill)
    end)
end

-- 神器技能重置
function ClassRef:C2S_RecycleLegendSkills(legend)
    local params = {ID=legend:getId()}
    app.session:request("pb_legend.C2S_RecycleLegendSkills",params,function(msg)
        -- dump(msg, "C2S_RecycleLegendSkills", 10)

        legend:resetSkills(msg.Num)
        self:checkRedPoint()
        app:sendMsg("LegendController", "onLegendSkillReset", legend)
    end)
end

-- 神器星魂升级
function ClassRef:C2S_ActiveLegendStarMapSkill(starMap, star)
    local params = {ID=star:getId()}
    app.session:request("pb_legend.C2S_ActiveLegendStarMapSkill",params,function(msg)
        -- dump(msg, "C2S_ActiveLegendStarMapSkill", 10)

        self._legendInfo:updateStarMapPg(starMap, star, msg)
        app:sendMsg("LegendController", "onLegendStarUpdate", starMap, star)
    end)
end

-- 神器外显激活
function ClassRef:C2S_MeltingLegend(legend, fashion)
    local params = {ID=fashion:getId()}
    app.session:request("pb_legend.C2S_MeltingLegend",params,function(msg)
        -- dump(msg, "C2S_MeltingLegend", 10)

        legend:updateFashion(fashion)
        app:sendMsg("LegendController", "onLegendFashionActive", legend, fashion)
    end)
end

return ClassRef
